<%-- 分页按钮插件 --%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%!private static String clear(String queryString) {

		if (queryString == null || queryString.isEmpty())
			return "page=?";
		StringBuilder params = new StringBuilder("page=?");
		String[] arr = queryString.replace("?", "%3F").split("&");
		for (String p : arr) {
			if (p.isEmpty() || p.matches("page=.*"))
				continue;
			params.append("&").append(p);
		}
		return params.toString();
	}%>
<%!private static String build(String queryString, Integer page) {

		return queryString.replace("?", page.toString());
	}%>
<%!private static int getInt(HttpServletRequest request, String name) {

		String value = request.getParameter(name);
		if (value == null)
			return 0;
		try {
			return Integer.parseInt(value);
		} catch (RuntimeException e) {
			return 0;
		}
	}%>
<%
	String query = clear(request.getQueryString());
	int current = getInt(request, "page");
	int size = getInt(request, "size");
	int total = getInt(request, "total");
	int btn = 2;
	int first = 1;
	int last = (int) Math.ceil(total / (double) size);
%>
<%
	if (current > 1 || last > 1) {
%>
<nav>
	<ul class="pagination pagination-sm" style="margin: 0;">
		<%
			if (current > first) {
		%>
		<li>
			<a href="?<%=build(query, current - 1)%>">&lt;</a>
		</li>
		<%
			} else {
		%>
		<li class="disabled">
			<a href="javascript:;">&lt;</a>
		</li>
		<%
			}
		%>
	</ul>
	<%
		if (current - btn > first) {
	%>
	<ul class="pagination pagination-sm" style="margin: 0;">
		<li>
			<a href="?<%=build(query, first)%>"><%=first%>...
			</a>
		</li>
	</ul>
	<%
		}
	%>
	<ul class="pagination pagination-sm" style="margin: 0;">
		<%
			for (int i = btn; i > 0; i--) {
					int p = current - i;
					if (p < first) {
						continue;
					}
		%>
		<li>
			<a href="?<%=build(query, p)%>"><%=p%></a>
		</li>
		<%
			}
		%>
		<li class="active">
			<a href="javascript:;">
				<%=current%>
				<span class="sr-only"></span>
			</a>
		</li>
		<%
			for (int i = 1; i <= btn; i++) {
					int p = current + i;
					if (p > last) {
						break;
					}
		%>
		<li>
			<a href="?<%=build(query, p)%>"><%=p%></a>
		</li>
		<%
			}
		%>
	</ul>
	<%
		if (current + btn < last) {
	%>
	<ul class="pagination pagination-sm" style="margin: 0;">
		<li>
			<a href="?<%=build(query, last)%>">
				...<%=last%></a>
		</li>
	</ul>
	<%
		}
	%>
	<ul class="pagination pagination-sm" style="margin: 0;">
		<%
			if (current < last) {
		%>
		<li>
			<a href="?<%=build(query, current + 1)%>">&gt;</a>
		</li>
		<%
			} else {
		%>
		<li class="disabled">
			<a href="javascript:;">&gt;</a>
		</li>
		<%
			}
		%>
	</ul>
</nav>
<%
	}
%>